<?php
defined('IN_IA') or exit('Access Denied');


class manger {

    public $w = array();
    public $gpc = array();
    public $error;

    public  $pageSize = 20;

    protected $num = array(
        'ph0001.hezyq.com'  => 3,
        'www.kz1688.cn'     => 100,
        'm.juxiaoli.cc'     => 3,
        'diancan.wmmob.cn'  => 150
    );

    public function __construct()
    {
        global $_W;
        $basename = pathinfo($_W['siteroot']);
        $number = $this->num[$basename['basename']] ? : 2;
        if (moreOpen()){
            $num = p_getcolumn('multiple', array(), "COUNT(*)");
            if (!p_get('multiple', array('uniacid' => UNIACID))) {
                p_insert('multiple', array('uniacid' => UNIACID, 'created' => time()));
            }
        }

    }

    /**
     * @param $table
     * @param $page
     * @param $where
     * @param string $sequence
     * @param string $desc
     * @return array
     */
    public function getTableList($table, $page, $where, $sequence = 'id', $desc = "DESC")
    {
        $table  = prefix($table, true);
        $page   = max(1, intval($page));
        //echo 'SELECT * FROM '.$table." WHERE {$where} ORDER BY {$sequence} {$desc} LIMIT ".($page - 1) * $this->pageSize.','.$this->pageSize;
        $list   = p_fetchall('SELECT * FROM '.$table." WHERE {$where} ORDER BY {$sequence} {$desc} LIMIT ".($page - 1) * $this->pageSize.','.$this->pageSize);
        $total  = p_fetchcolumn("SELECT COUNT(*) FROM {$table} WHERE {$where}");
        $pager = pagination($total, $page, $this->pageSize);
        return array(
            'list' => $list,
            'pages' => $pager,
            'total' => $total
        );
    }


    /**
     *
     * 一天的时间
     * @param   $date       开始 时间
     * @param   $endDate    结束 时间
     * @param   $filed      字段
     * @return  string
     */
    public function getTime($date ,$endDate, $filed)
    {
        if ($date == $endDate) {
            $state_time = strtotime($date . ' 00:00:01');
            $end_time   = strtotime($endDate . '23:59:59');
        } else {
            $state_time = strtotime($date);
            $end_time   = strtotime($endDate);
        }
        $where = "{$filed} BETWEEN {$state_time} AND {$end_time}";
        return $where ;
    }


    /**
     * 生成夺宝码
     *
     * @param $member_id
     * @param $treasure_id
     * @param $treasure_member_id
     * @param $number
     * @return bool
     */
    public function create_treasure_code($member_id, $treasure_id, $treasure_member_id, $number)
    {
        $data = array(
            'member_id' => $member_id,
            'treasure_id' => $treasure_id,
            'treasure_member_id' => $treasure_member_id,
            'created' => time()
        );
        for ($i = 0; $i < $number; $i++) {
            if (!p_insert('treasure_code', $data)) {
                $this->error = '生成夺宝码失败';
                return false;
            }
            $id = pdo_insertid();
            $no = $id . $treasure_id . $treasure_member_id;
            $content = str_pad($no, 10, '0', STR_PAD_LEFT);

            if (!p_update('treasure_code', array('code' => $content), array('id' => $id))) {
                $this->error = '生成夺宝码失败';
                return false;
            }
        }
        return true;
    }


    /**
     * 开奖
     */
    public function openTreasure($id)
    {
        $treasure = p_get('treasure', array('id' => $id, 'is_delete' => 1, 'is_down' => 1, 'status' => 1));
        if (empty($treasure)) {
            $this->error = '改夺宝不存在或者已经结束';
            return false;
        }
        $code = p_get('treasure_code', array('treasure_id' => $id, 'is_appoint' => 2));
        if (empty($code)) {
            $sql = 'SELECT * FROM ' . prefix('treasure_code') . ' WHERE treasure_id=' . $id . ' ORDER BY RAND()';
            $code = p_fetch($sql);
        }

        pdo_begin();
        try {
            if (!p_update('treasure_code', array('in_prize' => 2), array('id' => $code['id']))) {
                throw new Exception('开奖失败');
            }
            $order = array(
                'member_id' => $code['member_id'],
                'treasure_id' => $id,
                'treasure_code_id' => $code['id'],
                'code' => $code['code'],
                'created' => time()
            );

            if ($treasure['prize_type'] == 2) {
                $goods_fictitious = p_get('goods_fictitious', array('goods_id' => $treasure['id'], 'type' => 3));
                $order['fictitious'] = $goods_fictitious['content'];
                $order['prize_type'] = 2;
                if (!p_update('goods_fictitious', array('status' => 2, 'member_id' => $code['member_id']), array('id' => $goods_fictitious['id'], 'status' => 1))) {
                    throw new Exception('开奖失败');
                }

                $voucher = array(
                    'member_id' => $code['member_id'],
                    'shop_id' => $treasure['shop_id'],
                    'goods_id' => $treasure['id'],
                    'content' => $goods_fictitious['content'],
                    'type' => 3,
                    'created' => time()
                );

                if (!p_insert('my_voucher', $voucher)) {
                    throw new Exception('开奖失败');
                }

            } else {
                $address = p_get('address', array('member_id' => $code['member_id']));
                if (!empty($address)) {
                    $order['name'] = $address['name'];
                    $order['phone'] = $address['phone'];
                    $order['address'] = $address['address'];
                }
            }

            if (!p_insert('treasure_order', $order)) {
                throw new Exception('开奖失败');
            }
            if (!p_update('treasure', array('status' => 2, 'lottery_time' => time()), array('id' => $id))) {
                throw new Exception('开奖失败');
            }
            pdo_commit();
            return true;
        } catch (Exception $e) {
            pdo_rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 获取用户ids
     * @param $nickname
     * @return bool|string
     */
    public function getMemberId($nickname)
    {
        global $_GPC, $_W;
        $this->gpc = $_GPC;
        $this->w = $_W;
        $table = prefix('member');
        $where = " uniacid = {$this->w['uniacid']} AND nickname LIKE '%{$nickname}%'";
        $member = p_fetchall("SELECT * FROM {$table} WHERE  {$where}");
        if ($member) {
            foreach ($member as $item) {
                $member_id[] = $item['id'];
            }
        }
        if ($member_id) {
            return implode(',', $member_id);
        }
        return false;

    }




}